package com.example.myapplication;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Button button;
    TextView text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button =findViewById(R.id.button1);
                text=findViewById(R.id.text1);
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Uri uri=Uri.parse("content://com.ddd");
                        ContentResolver contentResolver=getContentResolver();
                        Cursor cur = contentResolver.query(uri,new String[]{"name"},null,null,null);
                       cur.moveToFirst();
                       String str  = cur.getString(0);
                        Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
                    }
                });
        text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myDbhelper dbOpenHelper10=new myDbhelper(MainActivity.this,"taniyu",null,1);
                SQLiteDatabase db10=dbOpenHelper10.getWritableDatabase();

                db10.beginTransaction();//开启事务
                int aa=0;
                try {
                    for (int i=0;i<10;i++) {
                        aa++;
                        ContentValues values = new ContentValues();
                        values.put("Name",i+"红孩儿");

                        db10.insert("work",null,values);
//                        db10.execSQL("insert into User(Name,sex,Age) values('"+i+"风清扬','男',108+i)");
                    }
                    db10.setTransactionSuccessful();//成功

                }catch (Exception e){
                    e.printStackTrace();
                }finally {
                    ((TextView)v).setText(aa+"");
                    db10.endTransaction();//结束事务
                    db10.close();//关闭
                }
            }
        });
    }
}
